package com.leyou.controller;



import com.leyou.search.pojo.SpecGroup;
import com.leyou.search.pojo.SpecGroupDTO;
import com.leyou.search.pojo.SpecParam;
import com.leyou.search.pojo.SpecParamDTO;
import com.leyou.service.IgroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("spec")
public class GroupController {
    @Autowired
    private IgroupService groupService;

    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> queryGroupByCategory(@RequestParam("id")Long id){
        return ResponseEntity.ok(this.groupService.queryGroupByCategory(id));
    }

    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParams(
            @RequestParam(value = "gid",required = false)Long gid,
            @RequestParam(value = "cid",required = false)Long cid,
            @RequestParam(value = "searching",required = false)Boolean searching)
    {
        return ResponseEntity.ok(this.groupService.querySpecParams(gid,cid,searching));
    }

    @PostMapping("group")
    public ResponseEntity<Void> addGroupByGategory(@RequestBody SpecGroup specGroup){
        groupService.addGroupByGategory(specGroup);
        return ResponseEntity.ok().build();

    }

    @PostMapping("param")
    public ResponseEntity<Void> addGroupParamByGategory(@RequestBody SpecParam specParam){
        groupService.addGroupParamByGategory(specParam);
        return ResponseEntity.ok().build();

    }


    @DeleteMapping("group/{id}")
    public ResponseEntity<Void> deleteGroupByGategory(@PathVariable("id")Long id){
        groupService.deleteGroupByGategory(id);
        return ResponseEntity.ok().build();

    }

    @DeleteMapping("param/{id}")
    public ResponseEntity<Void> deleteGroupParamByGategory(@PathVariable("id")Long id){
        groupService.deleteGroupParamByGategory(id);
        return ResponseEntity.ok().build();

    }

    @PutMapping("group")
    public ResponseEntity<Void> updateGroupByGategory(@RequestBody SpecGroup specGroup){
        groupService.updateGroupByGategory(specGroup);
        return ResponseEntity.ok().build();

    }

    @PutMapping("param")
    public ResponseEntity<Void> updateGroupParamByGategory(@RequestBody SpecParam specParam){
        groupService.updateGroupParamByGategory(specParam);
        return ResponseEntity.ok().build();

    }

    /**
     * 查询规格参数组，及组内参数
     * @param cid 商品分类id
     * @return 规格组及组内参数
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querySpecsByCid(@RequestParam("id") Long cid){
            return ResponseEntity.ok(groupService.querySpecsByCid(cid));
    }

}
